let backgroud = chrome.extension.getBackgroundPage();

let app = new Vue({
    el: "#app",
    data() {
        return {
            selectIndex: 0,
            bmList: [],
            tagsAll: [],
            searchTxt: "",
        }
    },
    watch: {
        searchTxt: function (val, oldVal) {
            this.search(val);
        },
    },
    methods: {
        search() {
            this.selectIndex = 0;
            this.bmList = backgroud.queryBmList(this.searchTxt, true);
            this.tagsAll = backgroud.tagsAll;
        },
        clickTag(tag) {
            this.searchTxt = tag;
            this.search();
        },
        nextLink() {
            this.selectIndex < this.bmList.length - 1 && this.selectIndex++;
        },
        lastLink() {
            this.selectIndex > 0 && this.selectIndex--;
        },
        goUrl(bm) {
            let url = bm.content;
            window.close();
            let queryOptions = { active: true };
            chrome.tabs.query(queryOptions, function (result) {
                chrome.tabs.update(result[0].id, { url });
            });

            // 写文件访问次数
            let cacheKey = "bm.click.num";
            chrome.storage.local.get(cacheKey, (result) => {
                let cache = result[cacheKey];
                if (cache) {
                    let match = false;
                    for (let bmCache of cache) {
                        if (bmCache.id == bm.id) {
                            bmCache.num++;
                            match = true;
                            break;
                        }
                    }
                    if (!match) {
                        cache.push({ id: bm.id, num: 1, bm });
                    }
                } else {
                    cache = [];
                    cache.push({ id: bm.id, num: 1, bm });
                }

                cache = cache.sort((a, b) => b.num - a.num).slice(0, 9);

                let cacheItem = {};
                cacheItem[cacheKey] = cache;
                chrome.storage.local.set(cacheItem, () => { });
            });

        },
        goFirst() {
            if (this.bmList && this.bmList.length > 0) {
                this.goUrl(this.bmList[this.selectIndex]);
            }
        },
        refresh() {
            backgroud.loadBm((bmList) => {
                chrome.notifications.create(null, {
                    type: 'basic',
                    iconUrl: 'img/icon48.png',
                    title: '提示信息',
                    message: `chrome书签缓存加载完成,size=${bmList.length}`
                });
            });
        },
        loadData() {
            // this.search();
            this.tagsAll = backgroud.tagsAll;

            let cacheKey = "bm.click.num";
            chrome.storage.local.get(cacheKey, (result) => {
                let cache = result[cacheKey];
                if (cache && cache.length > 0) {
                    app.bmList = cache.map(c => c.bm);
                }
            });

        },
        bmDataHandle() {

        }
    },
    mounted() {
        this.$refs.inp.focus();
        this.loadData();
    },
});

document.addEventListener('keyup', (event) => {
    if (event.code == 'ArrowUp') {
        app.lastLink();
    } else if (event.code == 'ArrowDown') {
        app.nextLink();
    } else if (event.code == 'Enter') {
        app.goFirst();
    }
});
